package v1

import (
	"net/http"

	"gitee.com/daonna/collab-manage-system/service"
	"github.com/gin-gonic/gin"
)

// CreateActivityReview 创建活动评论
func CreateActivityReview(c *gin.Context) {
	// 获取当前用户ID
	currentUserID := c.GetUint("user_id")

	// 获取活动ID
	activityID := c.Param("id")

	// 获取请求参数
	var input service.CreateActivityReviewInput
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "请求参数错误",
		})
		return
	}

	// 调用服务层创建活动评论
	review, err := service.CreateActivityReview(c.Request.Context(), currentUserID, activityID, input)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "创建活动评论失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "活动评论创建成功",
		"data":    review,
	})
}

// ListActivityReviews 获取活动评论列表
func ListActivityReviews(c *gin.Context) {
	// 获取活动ID
	activityID := c.Param("id")

	// 调用服务层获取活动评论列表
	reviews, err := service.ListActivityReviews(c.Request.Context(), activityID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "获取活动评论列表失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "活动评论列表获取成功",
		"data":    reviews,
	})

}

// UpdateActivityReview 更新活动评论
func UpdateActivityReview(c *gin.Context) {
	// 获取活动ID和评论ID
	activityID := c.Param("id")
	reviewID := c.Param("review_id")
	currentUserID := c.GetUint("user_id")
	// 获取请求参数
	var input service.UpdateActivityReviewInput
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "请求参数错误",
		})
		return
	}

	// 调用服务层更新活动评论
	review, err := service.UpdateActivityReview(c.Request.Context(), currentUserID, activityID, reviewID, input)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "更新活动评论失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "活动评论更新成功",
		"data":    review,
	})

}

// DeleteActivityReview 删除活动评论
func DeleteActivityReview(c *gin.Context) {
	// 获取活动ID和评论ID
	activityID := c.Param("id")
	reviewID := c.Param("review_id")
	currentUserID := c.GetUint("user_id")

	// 调用服务层删除活动评论
	err := service.DeleteActivityReview(c.Request.Context(), currentUserID, activityID, reviewID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "删除活动评论失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "活动评论删除成功",
	})
}
